<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Quickstart tutorial &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../about.html" >
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../index.html" >
    <link rel="up" title="NumPy User Guide" href="index.html" >
    <link rel="next" title="NumPy basics" href="basics.html" >
    <link rel="prev" title="Installing NumPy" href="install.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../index.html">
      <img border=0 alt="NumPy" src="../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="index.html" accesskey="U">NumPy User Guide</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="basics.html" title="NumPy basics"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="install.html" title="Installing NumPy"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Quickstart tutorial</a><ul>
<li><a class="reference internal" href="#prerequisites">Prerequisites</a></li>
<li><a class="reference internal" href="#the-basics">The Basics</a><ul>
<li><a class="reference internal" href="#an-example">An example</a></li>
<li><a class="reference internal" href="#array-creation">Array Creation</a></li>
<li><a class="reference internal" href="#printing-arrays">Printing Arrays</a></li>
<li><a class="reference internal" href="#basic-operations">Basic Operations</a></li>
<li><a class="reference internal" href="#universal-functions">Universal Functions</a></li>
<li><a class="reference internal" href="#indexing-slicing-and-iterating">Indexing, Slicing and Iterating</a></li>
</ul>
</li>
<li><a class="reference internal" href="#shape-manipulation">Shape Manipulation</a><ul>
<li><a class="reference internal" href="#changing-the-shape-of-an-array">Changing the shape of an array</a></li>
<li><a class="reference internal" href="#stacking-together-different-arrays">Stacking together different arrays</a></li>
<li><a class="reference internal" href="#splitting-one-array-into-several-smaller-ones">Splitting one array into several smaller ones</a></li>
</ul>
</li>
<li><a class="reference internal" href="#copies-and-views">Copies and Views</a><ul>
<li><a class="reference internal" href="#no-copy-at-all">No Copy at All</a></li>
<li><a class="reference internal" href="#view-or-shallow-copy">View or Shallow Copy</a></li>
<li><a class="reference internal" href="#deep-copy">Deep Copy</a></li>
<li><a class="reference internal" href="#functions-and-methods-overview">Functions and Methods Overview</a></li>
</ul>
</li>
<li><a class="reference internal" href="#less-basic">Less Basic</a><ul>
<li><a class="reference internal" href="#broadcasting-rules">Broadcasting rules</a></li>
</ul>
</li>
<li><a class="reference internal" href="#fancy-indexing-and-index-tricks">Fancy indexing and index tricks</a><ul>
<li><a class="reference internal" href="#indexing-with-arrays-of-indices">Indexing with Arrays of Indices</a></li>
<li><a class="reference internal" href="#indexing-with-boolean-arrays">Indexing with Boolean Arrays</a></li>
<li><a class="reference internal" href="#the-ix-function">The ix_() function</a></li>
<li><a class="reference internal" href="#indexing-with-strings">Indexing with strings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#linear-algebra">Linear Algebra</a><ul>
<li><a class="reference internal" href="#simple-array-operations">Simple Array Operations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#tricks-and-tips">Tricks and Tips</a><ul>
<li><a class="reference internal" href="#automatic-reshaping">“Automatic” Reshaping</a></li>
<li><a class="reference internal" href="#vector-stacking">Vector Stacking</a></li>
<li><a class="reference internal" href="#histograms">Histograms</a></li>
</ul>
</li>
<li><a class="reference internal" href="#further-reading">Further reading</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="install.html"
                        title="previous chapter">Installing NumPy</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="basics.html"
                        title="next chapter">NumPy basics</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="quickstart-tutorial">
<h1>Quickstart tutorial<a class="headerlink" href="#quickstart-tutorial" title="Permalink to this headline">¶</a></h1>
<div class="section" id="prerequisites">
<h2>Prerequisites<a class="headerlink" href="#prerequisites" title="Permalink to this headline">¶</a></h2>
<p>Before reading this tutorial you should know a bit of Python. If you
would like to refresh your memory, take a look at the <a class="reference external" href="https://docs.python.org/tutorial/">Python
tutorial</a>.</p>
<p>If you wish to work the examples in this tutorial, you must also have
some software installed on your computer. Please see
<a class="reference external" href="https://scipy.org/install.html">https://scipy.org/install.html</a> for instructions.</p>
</div>
<div class="section" id="the-basics">
<h2>The Basics<a class="headerlink" href="#the-basics" title="Permalink to this headline">¶</a></h2>
<p>NumPy’s main object is the homogeneous multidimensional array. It is a
table of elements (usually numbers), all of the same type, indexed by a
tuple of non-negative integers. In NumPy dimensions are called <em>axes</em>.</p>
<p>For example, the coordinates of a point in 3D space <code class="docutils literal notranslate"><span class="pre">[1, 2, 1]</span></code> has
one axis. That axis has 3 elements in it, so we say it has a length
of 3. In the example pictured below, the array has 2 axes. The first
axis has a length of 2, the second axis has a length of 3.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[[</span> <span class="mf">1.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">0.</span><span class="p">],</span>
 <span class="p">[</span> <span class="mf">0.</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">]]</span>
</pre></div>
</div>
<p>NumPy’s array class is called <code class="docutils literal notranslate"><span class="pre">ndarray</span></code>. It is also known by the alias
<code class="docutils literal notranslate"><span class="pre">array</span></code>. Note that <code class="docutils literal notranslate"><span class="pre">numpy.array</span></code> is not the same as the Standard
Python Library class <code class="docutils literal notranslate"><span class="pre">array.array</span></code>, which only handles one-dimensional
arrays and offers less functionality. The more important attributes of
an <code class="docutils literal notranslate"><span class="pre">ndarray</span></code> object are:</p>
<dl class="simple">
<dt>ndarray.ndim</dt><dd><p>the number of axes (dimensions) of the array.</p>
</dd>
<dt>ndarray.shape</dt><dd><p>the dimensions of the array. This is a tuple of integers indicating
the size of the array in each dimension. For a matrix with <em>n</em> rows
and <em>m</em> columns, <code class="docutils literal notranslate"><span class="pre">shape</span></code> will be <code class="docutils literal notranslate"><span class="pre">(n,m)</span></code>. The length of the
<code class="docutils literal notranslate"><span class="pre">shape</span></code> tuple is therefore the number of axes, <code class="docutils literal notranslate"><span class="pre">ndim</span></code>.</p>
</dd>
<dt>ndarray.size</dt><dd><p>the total number of elements of the array. This is equal to the
product of the elements of <code class="docutils literal notranslate"><span class="pre">shape</span></code>.</p>
</dd>
<dt>ndarray.dtype</dt><dd><p>an object describing the type of the elements in the array. One can
create or specify dtype’s using standard Python types. Additionally
NumPy provides types of its own. numpy.int32, numpy.int16, and
numpy.float64 are some examples.</p>
</dd>
<dt>ndarray.itemsize</dt><dd><p>the size in bytes of each element of the array. For example, an
array of elements of type <code class="docutils literal notranslate"><span class="pre">float64</span></code> has <code class="docutils literal notranslate"><span class="pre">itemsize</span></code> 8 (=64/8),
while one of type <code class="docutils literal notranslate"><span class="pre">complex32</span></code> has <code class="docutils literal notranslate"><span class="pre">itemsize</span></code> 4 (=32/8). It is
equivalent to <code class="docutils literal notranslate"><span class="pre">ndarray.dtype.itemsize</span></code>.</p>
</dd>
<dt>ndarray.data</dt><dd><p>the buffer containing the actual elements of the array. Normally, we
won’t need to use this attribute because we will access the elements
in an array using indexing facilities.</p>
</dd>
</dl>
<div class="section" id="an-example">
<h3>An example<a class="headerlink" href="#an-example" title="Permalink to this headline">¶</a></h3>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 0,  1,  2,  3,  4],</span>
<span class="go">       [ 5,  6,  7,  8,  9],</span>
<span class="go">       [10, 11, 12, 13, 14]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 5)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">ndim</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;int64&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">itemsize</span>
<span class="go">8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">size</span>
<span class="go">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">&lt;type &#39;numpy.ndarray&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([6, 7, 8])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">&lt;type &#39;numpy.ndarray&#39;&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="array-creation">
<h3>Array Creation<a class="headerlink" href="#array-creation" title="Permalink to this headline">¶</a></h3>
<p>There are several ways to create arrays.</p>
<p>For example, you can create an array from a regular Python list or tuple
using the <code class="docutils literal notranslate"><span class="pre">array</span></code> function. The type of the resulting array is deduced
from the type of the elements in the sequences.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([2, 3, 4])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">dtype</span>
<span class="go">dtype(&#39;int64&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.2</span><span class="p">,</span> <span class="mf">3.5</span><span class="p">,</span> <span class="mf">5.1</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">dtype</span>
<span class="go">dtype(&#39;float64&#39;)</span>
</pre></div>
</div>
<p>A frequent error consists in calling <code class="docutils literal notranslate"><span class="pre">array</span></code> with multiple numeric
arguments, rather than providing a single list of numbers as an
argument.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>    <span class="c1"># WRONG</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>  <span class="c1"># RIGHT</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">array</span></code> transforms sequences of sequences into two-dimensional arrays,
sequences of sequences of sequences into three-dimensional arrays, and
so on.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([(</span><span class="mf">1.5</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([[ 1.5,  2. ,  3. ],</span>
<span class="go">       [ 4. ,  5. ,  6. ]])</span>
</pre></div>
</div>
<p>The type of the array can also be explicitly specified at creation time:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span> <span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">complex</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
<span class="go">array([[ 1.+0.j,  2.+0.j],</span>
<span class="go">       [ 3.+0.j,  4.+0.j]])</span>
</pre></div>
</div>
<p>Often, the elements of an array are originally unknown, but its size is
known. Hence, NumPy offers several functions to create
arrays with initial placeholder content. These minimize the necessity of
growing arrays, an expensive operation.</p>
<p>The function <code class="docutils literal notranslate"><span class="pre">zeros</span></code> creates an array full of zeros, the function
<code class="docutils literal notranslate"><span class="pre">ones</span></code> creates an array full of ones, and the function <code class="docutils literal notranslate"><span class="pre">empty</span></code>
creates an array whose initial content is random and depends on the
state of the memory. By default, the dtype of the created array is
<code class="docutils literal notranslate"><span class="pre">float64</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span> <span class="p">)</span>
<span class="go">array([[ 0.,  0.,  0.,  0.],</span>
<span class="go">       [ 0.,  0.,  0.,  0.],</span>
<span class="go">       [ 0.,  0.,  0.,  0.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int16</span> <span class="p">)</span>                <span class="c1"># dtype can also be specified</span>
<span class="go">array([[[ 1, 1, 1, 1],</span>
<span class="go">        [ 1, 1, 1, 1],</span>
<span class="go">        [ 1, 1, 1, 1]],</span>
<span class="go">       [[ 1, 1, 1, 1],</span>
<span class="go">        [ 1, 1, 1, 1],</span>
<span class="go">        [ 1, 1, 1, 1]]], dtype=int16)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="p">)</span>                                 <span class="c1"># uninitialized, output may vary</span>
<span class="go">array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],</span>
<span class="go">       [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])</span>
</pre></div>
</div>
<p>To create sequences of numbers, NumPy provides a function analogous to
<code class="docutils literal notranslate"><span class="pre">range</span></code> that returns arrays instead of lists.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">5</span> <span class="p">)</span>
<span class="go">array([10, 15, 20, 25])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">0.3</span> <span class="p">)</span>                 <span class="c1"># it accepts float arguments</span>
<span class="go">array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])</span>
</pre></div>
</div>
<p>When <code class="docutils literal notranslate"><span class="pre">arange</span></code> is used with floating point arguments, it is generally
not possible to predict the number of elements obtained, due to the
finite floating point precision. For this reason, it is usually better
to use the function <code class="docutils literal notranslate"><span class="pre">linspace</span></code> that receives as an argument the number
of elements that we want, instead of the step:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">9</span> <span class="p">)</span>                 <span class="c1"># 9 numbers from 0 to 2</span>
<span class="go">array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">pi</span><span class="p">,</span> <span class="mi">100</span> <span class="p">)</span>        <span class="c1"># useful to evaluate function at lots of points</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../reference/generated/numpy.array.html#numpy.array" title="numpy.array"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.zeros.html#numpy.zeros" title="numpy.zeros"><code class="xref py py-obj docutils literal notranslate"><span class="pre">zeros</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.zeros_like.html#numpy.zeros_like" title="numpy.zeros_like"><code class="xref py py-obj docutils literal notranslate"><span class="pre">zeros_like</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ones.html#numpy.ones" title="numpy.ones"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ones</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ones_like.html#numpy.ones_like" title="numpy.ones_like"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ones_like</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.empty.html#numpy.empty" title="numpy.empty"><code class="xref py py-obj docutils literal notranslate"><span class="pre">empty</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.empty_like.html#numpy.empty_like" title="numpy.empty_like"><code class="xref py py-obj docutils literal notranslate"><span class="pre">empty_like</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.arange.html#numpy.arange" title="numpy.arange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">arange</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.linspace.html#numpy.linspace" title="numpy.linspace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">linspace</span></code></a>,
<a class="reference internal" href="../reference/random/generated/numpy.random.RandomState.rand.html#numpy.random.RandomState.rand" title="numpy.random.RandomState.rand"><code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.random.RandomState.rand</span></code></a>,
<a class="reference internal" href="../reference/random/generated/numpy.random.RandomState.randn.html#numpy.random.RandomState.randn" title="numpy.random.RandomState.randn"><code class="xref py py-obj docutils literal notranslate"><span class="pre">numpy.random.RandomState.randn</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.fromfunction.html#numpy.fromfunction" title="numpy.fromfunction"><code class="xref py py-obj docutils literal notranslate"><span class="pre">fromfunction</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.fromfile.html#numpy.fromfile" title="numpy.fromfile"><code class="xref py py-obj docutils literal notranslate"><span class="pre">fromfile</span></code></a></p>
</div>
</div>
<div class="section" id="printing-arrays">
<h3>Printing Arrays<a class="headerlink" href="#printing-arrays" title="Permalink to this headline">¶</a></h3>
<p>When you print an array, NumPy displays it in a similar way to nested
lists, but with the following layout:</p>
<ul class="simple">
<li><p>the last axis is printed from left to right,</p></li>
<li><p>the second-to-last is printed from top to bottom,</p></li>
<li><p>the rest are also printed from top to bottom, with each slice
separated from the next by an empty line.</p></li>
</ul>
<p>One-dimensional arrays are then printed as rows, bidimensionals as
matrices and tridimensionals as lists of matrices.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>                         <span class="c1"># 1d array</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">[0 1 2 3 4 5]</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>           <span class="c1"># 2d array</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">[[ 0  1  2]</span>
<span class="go"> [ 3  4  5]</span>
<span class="go"> [ 6  7  8]</span>
<span class="go"> [ 9 10 11]]</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>         <span class="c1"># 3d array</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="go">[[[ 0  1  2  3]</span>
<span class="go">  [ 4  5  6  7]</span>
<span class="go">  [ 8  9 10 11]]</span>
<span class="go"> [[12 13 14 15]</span>
<span class="go">  [16 17 18 19]</span>
<span class="go">  [20 21 22 23]]]</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="#quickstart-shape-manipulation"><span class="std std-ref">below</span></a> to get
more details on <code class="docutils literal notranslate"><span class="pre">reshape</span></code>.</p>
<p>If an array is too large to be printed, NumPy automatically skips the
central part of the array and only prints the corners:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10000</span><span class="p">))</span>
<span class="go">[   0    1    2 ..., 9997 9998 9999]</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10000</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span><span class="mi">100</span><span class="p">))</span>
<span class="go">[[   0    1    2 ...,   97   98   99]</span>
<span class="go"> [ 100  101  102 ...,  197  198  199]</span>
<span class="go"> [ 200  201  202 ...,  297  298  299]</span>
<span class="go"> ...,</span>
<span class="go"> [9700 9701 9702 ..., 9797 9798 9799]</span>
<span class="go"> [9800 9801 9802 ..., 9897 9898 9899]</span>
<span class="go"> [9900 9901 9902 ..., 9997 9998 9999]]</span>
</pre></div>
</div>
<p>To disable this behaviour and force NumPy to print the entire array, you
can change the printing options using <code class="docutils literal notranslate"><span class="pre">set_printoptions</span></code>.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">set_printoptions</span><span class="p">(</span><span class="n">threshold</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span><span class="p">)</span>       <span class="c1"># sys module should be imported</span>
</pre></div>
</div>
</div>
<div class="section" id="basic-operations">
<h3>Basic Operations<a class="headerlink" href="#basic-operations" title="Permalink to this headline">¶</a></h3>
<p>Arithmetic operators on arrays apply <em>elementwise</em>. A new array is
created and filled with the result.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span><span class="mi">30</span><span class="p">,</span><span class="mi">40</span><span class="p">,</span><span class="mi">50</span><span class="p">]</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span> <span class="mi">4</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([0, 1, 2, 3])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="o">-</span><span class="n">b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
<span class="go">array([20, 29, 38, 47])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">**</span><span class="mi">2</span>
<span class="go">array([0, 1, 4, 9])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">10</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">&lt;</span><span class="mi">35</span>
<span class="go">array([ True, True, False, False])</span>
</pre></div>
</div>
<p>Unlike in many matrix languages, the product operator <code class="docutils literal notranslate"><span class="pre">*</span></code> operates
elementwise in NumPy arrays. The matrix product can be performed using
the <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> operator (in python &gt;=3.5) or the <code class="docutils literal notranslate"><span class="pre">dot</span></code> function or method:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span>
<span class="gp">... </span>            <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">]]</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[[</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span>
<span class="gp">... </span>            <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]]</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">*</span> <span class="n">B</span>                       <span class="c1"># elementwise product</span>
<span class="go">array([[2, 0],</span>
<span class="go">       [0, 4]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span> <span class="o">@</span> <span class="n">B</span>                       <span class="c1"># matrix product</span>
<span class="go">array([[5, 4],</span>
<span class="go">       [3, 4]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>                    <span class="c1"># another matrix product</span>
<span class="go">array([[5, 4],</span>
<span class="go">       [3, 4]])</span>
</pre></div>
</div>
<p>Some operations, such as <code class="docutils literal notranslate"><span class="pre">+=</span></code> and <code class="docutils literal notranslate"><span class="pre">*=</span></code>, act in place to modify an
existing array rather than create a new one.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">*=</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[3, 3, 3],</span>
<span class="go">       [3, 3, 3]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">+=</span> <span class="n">a</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([[ 3.417022  ,  3.72032449,  3.00011437],</span>
<span class="go">       [ 3.30233257,  3.14675589,  3.09233859]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">+=</span> <span class="n">b</span>                  <span class="c1"># b is not automatically converted to integer type</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">Cannot cast ufunc add output from dtype(&#39;float64&#39;) to dtype(&#39;int64&#39;) with casting rule &#39;same_kind&#39;</span>
</pre></div>
</div>
<p>When operating with arrays of different types, the type of the resulting
array corresponds to the more general or precise one (a behavior known
as upcasting).</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="n">pi</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;float64&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
<span class="go">array([ 1.        ,  2.57079633,  4.14159265])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;float64&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">c</span><span class="o">*</span><span class="mi">1</span><span class="n">j</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j,</span>
<span class="go">       -0.54030231-0.84147098j])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">name</span>
<span class="go">&#39;complex128&#39;</span>
</pre></div>
</div>
<p>Many unary operations, such as computing the sum of all the elements in
the array, are implemented as methods of the <code class="docutils literal notranslate"><span class="pre">ndarray</span></code> class.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 0.18626021,  0.34556073,  0.39676747],</span>
<span class="go">       [ 0.53881673,  0.41919451,  0.6852195 ]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="go">2.5718191614547998</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">min</span><span class="p">()</span>
<span class="go">0.1862602113776709</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="go">0.6852195003967595</span>
</pre></div>
</div>
<p>By default, these operations apply to the array as though it were a list
of numbers, regardless of its shape. However, by specifying the <code class="docutils literal notranslate"><span class="pre">axis</span></code>
parameter you can apply an operation along the specified axis of an
array:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([[ 0,  1,  2,  3],</span>
<span class="go">       [ 4,  5,  6,  7],</span>
<span class="go">       [ 8,  9, 10, 11]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>                            <span class="c1"># sum of each column</span>
<span class="go">array([12, 15, 18, 21])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>                            <span class="c1"># min of each row</span>
<span class="go">array([0, 4, 8])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>                         <span class="c1"># cumulative sum along each row</span>
<span class="go">array([[ 0,  1,  3,  6],</span>
<span class="go">       [ 4,  9, 15, 22],</span>
<span class="go">       [ 8, 17, 27, 38]])</span>
</pre></div>
</div>
</div>
<div class="section" id="universal-functions">
<h3>Universal Functions<a class="headerlink" href="#universal-functions" title="Permalink to this headline">¶</a></h3>
<p>NumPy provides familiar mathematical functions such as sin, cos, and
exp. In NumPy, these are called “universal
functions”(<code class="docutils literal notranslate"><span class="pre">ufunc</span></code>). Within NumPy, these functions
operate elementwise on an array, producing an array as output.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">B</span>
<span class="go">array([0, 1, 2])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>
<span class="go">array([ 1.        ,  2.71828183,  7.3890561 ])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">B</span><span class="p">)</span>
<span class="go">array([ 0.        ,  1.        ,  1.41421356])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">2.</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="n">C</span><span class="p">)</span>
<span class="go">array([ 2.,  0.,  6.])</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../reference/generated/numpy.all.html#numpy.all" title="numpy.all"><code class="xref py py-obj docutils literal notranslate"><span class="pre">all</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.any.html#numpy.any" title="numpy.any"><code class="xref py py-obj docutils literal notranslate"><span class="pre">any</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.apply_along_axis.html#numpy.apply_along_axis" title="numpy.apply_along_axis"><code class="xref py py-obj docutils literal notranslate"><span class="pre">apply_along_axis</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.argmax.html#numpy.argmax" title="numpy.argmax"><code class="xref py py-obj docutils literal notranslate"><span class="pre">argmax</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.argmin.html#numpy.argmin" title="numpy.argmin"><code class="xref py py-obj docutils literal notranslate"><span class="pre">argmin</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.argsort.html#numpy.argsort" title="numpy.argsort"><code class="xref py py-obj docutils literal notranslate"><span class="pre">argsort</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.average.html#numpy.average" title="numpy.average"><code class="xref py py-obj docutils literal notranslate"><span class="pre">average</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.bincount.html#numpy.bincount" title="numpy.bincount"><code class="xref py py-obj docutils literal notranslate"><span class="pre">bincount</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ceil.html#numpy.ceil" title="numpy.ceil"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ceil</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.clip.html#numpy.clip" title="numpy.clip"><code class="xref py py-obj docutils literal notranslate"><span class="pre">clip</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.conj.html#numpy.conj" title="numpy.conj"><code class="xref py py-obj docutils literal notranslate"><span class="pre">conj</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.corrcoef.html#numpy.corrcoef" title="numpy.corrcoef"><code class="xref py py-obj docutils literal notranslate"><span class="pre">corrcoef</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.cov.html#numpy.cov" title="numpy.cov"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cov</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.cross.html#numpy.cross" title="numpy.cross"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cross</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.cumprod.html#numpy.cumprod" title="numpy.cumprod"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cumprod</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.cumsum.html#numpy.cumsum" title="numpy.cumsum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cumsum</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.diff.html#numpy.diff" title="numpy.diff"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diff</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.dot.html#numpy.dot" title="numpy.dot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dot</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.floor.html#numpy.floor" title="numpy.floor"><code class="xref py py-obj docutils literal notranslate"><span class="pre">floor</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.inner.html#numpy.inner" title="numpy.inner"><code class="xref py py-obj docutils literal notranslate"><span class="pre">inner</span></code></a>,
<em class="xref py py-obj">inv</em>,
<a class="reference internal" href="../reference/generated/numpy.lexsort.html#numpy.lexsort" title="numpy.lexsort"><code class="xref py py-obj docutils literal notranslate"><span class="pre">lexsort</span></code></a>,
<a class="reference external" href="https://docs.python.org/dev/library/functions.html#max" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">max</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.maximum.html#numpy.maximum" title="numpy.maximum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">maximum</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.mean.html#numpy.mean" title="numpy.mean"><code class="xref py py-obj docutils literal notranslate"><span class="pre">mean</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.median.html#numpy.median" title="numpy.median"><code class="xref py py-obj docutils literal notranslate"><span class="pre">median</span></code></a>,
<a class="reference external" href="https://docs.python.org/dev/library/functions.html#min" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">min</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.minimum.html#numpy.minimum" title="numpy.minimum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">minimum</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.nonzero.html#numpy.nonzero" title="numpy.nonzero"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nonzero</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.outer.html#numpy.outer" title="numpy.outer"><code class="xref py py-obj docutils literal notranslate"><span class="pre">outer</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.prod.html#numpy.prod" title="numpy.prod"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prod</span></code></a>,
<a class="reference external" href="https://docs.python.org/dev/library/re.html#module-re" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">re</span></code></a>,
<a class="reference external" href="https://docs.python.org/dev/library/functions.html#round" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">round</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.sort.html#numpy.sort" title="numpy.sort"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sort</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.std.html#numpy.std" title="numpy.std"><code class="xref py py-obj docutils literal notranslate"><span class="pre">std</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.sum.html#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sum</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.trace.html#numpy.trace" title="numpy.trace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">trace</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.transpose.html#numpy.transpose" title="numpy.transpose"><code class="xref py py-obj docutils literal notranslate"><span class="pre">transpose</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.var.html#numpy.var" title="numpy.var"><code class="xref py py-obj docutils literal notranslate"><span class="pre">var</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.vdot.html#numpy.vdot" title="numpy.vdot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vdot</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.vectorize.html#numpy.vectorize" title="numpy.vectorize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vectorize</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.where.html#numpy.where" title="numpy.where"><code class="xref py py-obj docutils literal notranslate"><span class="pre">where</span></code></a></p>
</div>
</div>
<div class="section" id="indexing-slicing-and-iterating">
<h3>Indexing, Slicing and Iterating<a class="headerlink" href="#indexing-slicing-and-iterating" title="Permalink to this headline">¶</a></h3>
<p><strong>One-dimensional</strong> arrays can be indexed, sliced and iterated over,
much like
<a class="reference external" href="https://docs.python.org/tutorial/introduction.html#lists">lists</a>
and other Python sequences.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">**</span><span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="go">8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>
<span class="go">array([ 8, 27, 64])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:</span><span class="mi">6</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1000</span>    <span class="c1"># equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([-1000,     1, -1000,    27, -1000,   125,   216,   343,   512,   729])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span> <span class="p">:</span> <span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>                                 <span class="c1"># reversed a</span>
<span class="go">array([  729,   512,   343,   216,   125, -1000,    27, -1000,     1, -1000])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">a</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="o">**</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="mf">3.</span><span class="p">))</span>
<span class="gp">...</span>
<span class="go">nan</span>
<span class="go">1.0</span>
<span class="go">nan</span>
<span class="go">3.0</span>
<span class="go">nan</span>
<span class="go">5.0</span>
<span class="go">6.0</span>
<span class="go">7.0</span>
<span class="go">8.0</span>
<span class="go">9.0</span>
</pre></div>
</div>
<p><strong>Multidimensional</strong> arrays can have one index per axis. These indices
are given in a tuple separated by commas:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="mi">10</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="n">y</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfunction</span><span class="p">(</span><span class="n">f</span><span class="p">,(</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span><span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([[ 0,  1,  2,  3],</span>
<span class="go">       [10, 11, 12, 13],</span>
<span class="go">       [20, 21, 22, 23],</span>
<span class="go">       [30, 31, 32, 33],</span>
<span class="go">       [40, 41, 42, 43]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
<span class="go">23</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>                       <span class="c1"># each row in the second column of b</span>
<span class="go">array([ 1, 11, 21, 31, 41])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span> <span class="p">:</span> <span class="p">,</span><span class="mi">1</span><span class="p">]</span>                        <span class="c1"># equivalent to the previous example</span>
<span class="go">array([ 1, 11, 21, 31, 41])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">,</span> <span class="p">:</span> <span class="p">]</span>                      <span class="c1"># each column in the second and third row of b</span>
<span class="go">array([[10, 11, 12, 13],</span>
<span class="go">       [20, 21, 22, 23]])</span>
</pre></div>
</div>
<p>When fewer indices are provided than the number of axes, the missing
indices are considered complete slices<code class="docutils literal notranslate"><span class="pre">:</span></code></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>                                  <span class="c1"># the last row. Equivalent to b[-1,:]</span>
<span class="go">array([40, 41, 42, 43])</span>
</pre></div>
</div>
<p>The expression within brackets in <code class="docutils literal notranslate"><span class="pre">b[i]</span></code> is treated as an <code class="docutils literal notranslate"><span class="pre">i</span></code>
followed by as many instances of <code class="docutils literal notranslate"><span class="pre">:</span></code> as needed to represent the
remaining axes. NumPy also allows you to write this using dots as
<code class="docutils literal notranslate"><span class="pre">b[i,...]</span></code>.</p>
<p>The <strong>dots</strong> (<code class="docutils literal notranslate"><span class="pre">...</span></code>) represent as many colons as needed to produce a
complete indexing tuple. For example, if <code class="docutils literal notranslate"><span class="pre">x</span></code> is an array with 5
axes, then</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">x[1,2,...]</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">x[1,2,:,:,:]</span></code>,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x[...,3]</span></code> to <code class="docutils literal notranslate"><span class="pre">x[:,:,:,:,3]</span></code> and</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x[4,...,5,:]</span></code> to <code class="docutils literal notranslate"><span class="pre">x[4,:,:,5,:]</span></code>.</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[[[</span>  <span class="mi">0</span><span class="p">,</span>  <span class="mi">1</span><span class="p">,</span>  <span class="mi">2</span><span class="p">],</span>               <span class="c1"># a 3D array (two stacked 2D arrays)</span>
<span class="gp">... </span>                <span class="p">[</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">]],</span>
<span class="gp">... </span>               <span class="p">[[</span><span class="mi">100</span><span class="p">,</span><span class="mi">101</span><span class="p">,</span><span class="mi">102</span><span class="p">],</span>
<span class="gp">... </span>                <span class="p">[</span><span class="mi">110</span><span class="p">,</span><span class="mi">112</span><span class="p">,</span><span class="mi">113</span><span class="p">]]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(2, 2, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="o">...</span><span class="p">]</span>                                   <span class="c1"># same as c[1,:,:] or c[1]</span>
<span class="go">array([[100, 101, 102],</span>
<span class="go">       [110, 112, 113]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="o">...</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span>                                   <span class="c1"># same as c[:,:,2]</span>
<span class="go">array([[  2,  13],</span>
<span class="go">       [102, 113]])</span>
</pre></div>
</div>
<p><strong>Iterating</strong> over multidimensional arrays is done with respect to the
first axis:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">b</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">[0 1 2 3]</span>
<span class="go">[10 11 12 13]</span>
<span class="go">[20 21 22 23]</span>
<span class="go">[30 31 32 33]</span>
<span class="go">[40 41 42 43]</span>
</pre></div>
</div>
<p>However, if one wants to perform an operation on each element in the
array, one can use the <code class="docutils literal notranslate"><span class="pre">flat</span></code> attribute which is an
<a class="reference external" href="https://docs.python.org/tutorial/classes.html#iterators">iterator</a>
over all the elements of the array:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">b</span><span class="o">.</span><span class="n">flat</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">element</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">0</span>
<span class="go">1</span>
<span class="go">2</span>
<span class="go">3</span>
<span class="go">10</span>
<span class="go">11</span>
<span class="go">12</span>
<span class="go">13</span>
<span class="go">20</span>
<span class="go">21</span>
<span class="go">22</span>
<span class="go">23</span>
<span class="go">30</span>
<span class="go">31</span>
<span class="go">32</span>
<span class="go">33</span>
<span class="go">40</span>
<span class="go">41</span>
<span class="go">42</span>
<span class="go">43</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="basics.indexing.html#basics-indexing"><span class="std std-ref">Indexing</span></a>,
<a class="reference internal" href="../reference/arrays.indexing.html#arrays-indexing"><span class="std std-ref">Indexing</span></a> (reference),
<a class="reference internal" href="../reference/constants.html#numpy.newaxis" title="numpy.newaxis"><code class="xref py py-obj docutils literal notranslate"><span class="pre">newaxis</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ndenumerate.html#numpy.ndenumerate" title="numpy.ndenumerate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndenumerate</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.indices.html#numpy.indices" title="numpy.indices"><code class="xref py py-obj docutils literal notranslate"><span class="pre">indices</span></code></a></p>
</div>
</div>
</div>
<div class="section" id="shape-manipulation">
<span id="quickstart-shape-manipulation"></span><h2>Shape Manipulation<a class="headerlink" href="#shape-manipulation" title="Permalink to this headline">¶</a></h2>
<div class="section" id="changing-the-shape-of-an-array">
<h3>Changing the shape of an array<a class="headerlink" href="#changing-the-shape-of-an-array" title="Permalink to this headline">¶</a></h3>
<p>An array has a shape given by the number of elements along each axis:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="mi">10</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 2.,  8.,  0.,  6.],</span>
<span class="go">       [ 4.,  5.,  1.,  1.],</span>
<span class="go">       [ 8.,  9.,  3.,  6.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 4)</span>
</pre></div>
</div>
<p>The shape of an array can be changed with various commands. Note that the
following three commands all return a modified array, but do not change
the original array:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>  <span class="c1"># returns the array, flattened</span>
<span class="go">array([ 2.,  8.,  0.,  6.,  4.,  5.,  1.,  1.,  8.,  9.,  3.,  6.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>  <span class="c1"># returns the array with a modified shape</span>
<span class="go">array([[ 2.,  8.],</span>
<span class="go">       [ 0.,  6.],</span>
<span class="go">       [ 4.,  5.],</span>
<span class="go">       [ 1.,  1.],</span>
<span class="go">       [ 8.,  9.],</span>
<span class="go">       [ 3.,  6.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">T</span>  <span class="c1"># returns the array, transposed</span>
<span class="go">array([[ 2.,  4.,  8.],</span>
<span class="go">       [ 8.,  5.,  9.],</span>
<span class="go">       [ 0.,  1.,  3.],</span>
<span class="go">       [ 6.,  1.,  6.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(4, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 4)</span>
</pre></div>
</div>
<p>The order of the elements in the array resulting from ravel() is
normally “C-style”, that is, the rightmost index “changes the fastest”,
so the element after a[0,0] is a[0,1]. If the array is reshaped to some
other shape, again the array is treated as “C-style”. NumPy normally
creates arrays stored in this order, so ravel() will usually not need to
copy its argument, but if the array was made by taking slices of another
array or created with unusual options, it may need to be copied. The
functions ravel() and reshape() can also be instructed, using an
optional argument, to use FORTRAN-style arrays, in which the leftmost
index changes the fastest.</p>
<p>The <a class="reference internal" href="../reference/generated/numpy.reshape.html#numpy.reshape" title="numpy.reshape"><code class="xref py py-obj docutils literal notranslate"><span class="pre">reshape</span></code></a> function returns its
argument with a modified shape, whereas the
<a class="reference internal" href="../reference/generated/numpy.ndarray.resize.html#numpy.ndarray.resize" title="numpy.ndarray.resize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndarray.resize</span></code></a> method modifies the array
itself:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 2.,  8.,  0.,  6.],</span>
<span class="go">       [ 4.,  5.,  1.,  1.],</span>
<span class="go">       [ 8.,  9.,  3.,  6.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">resize</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">6</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 2.,  8.,  0.,  6.,  4.,  5.],</span>
<span class="go">       [ 1.,  1.,  8.,  9.,  3.,  6.]])</span>
</pre></div>
</div>
<p>If a dimension is given as -1 in a reshaping operation, the other
dimensions are automatically calculated:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">array([[ 2.,  8.,  0.,  6.],</span>
<span class="go">       [ 4.,  5.,  1.,  1.],</span>
<span class="go">       [ 8.,  9.,  3.,  6.]])</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../reference/generated/numpy.ndarray.shape.html#numpy.ndarray.shape" title="numpy.ndarray.shape"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndarray.shape</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.reshape.html#numpy.reshape" title="numpy.reshape"><code class="xref py py-obj docutils literal notranslate"><span class="pre">reshape</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.resize.html#numpy.resize" title="numpy.resize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">resize</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ravel.html#numpy.ravel" title="numpy.ravel"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ravel</span></code></a></p>
</div>
</div>
<div class="section" id="stacking-together-different-arrays">
<h3>Stacking together different arrays<a class="headerlink" href="#stacking-together-different-arrays" title="Permalink to this headline">¶</a></h3>
<p>Several arrays can be stacked together along different axes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="mi">10</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 8.,  8.],</span>
<span class="go">       [ 0.,  0.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="mi">10</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">array([[ 1.,  8.],</span>
<span class="go">       [ 0.,  4.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">))</span>
<span class="go">array([[ 8.,  8.],</span>
<span class="go">       [ 0.,  0.],</span>
<span class="go">       [ 1.,  8.],</span>
<span class="go">       [ 0.,  4.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">hstack</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">))</span>
<span class="go">array([[ 8.,  8.,  1.,  8.],</span>
<span class="go">       [ 0.,  0.,  0.,  4.]])</span>
</pre></div>
</div>
<p>The function <a class="reference internal" href="../reference/generated/numpy.column_stack.html#numpy.column_stack" title="numpy.column_stack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">column_stack</span></code></a>
stacks 1D arrays as columns into a 2D array. It is equivalent to
<a class="reference internal" href="../reference/generated/numpy.hstack.html#numpy.hstack" title="numpy.hstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hstack</span></code></a> only for 2D arrays:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">newaxis</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">))</span>     <span class="c1"># with 2D arrays</span>
<span class="go">array([[ 8.,  8.,  1.,  8.],</span>
<span class="go">       [ 0.,  0.,  0.,  4.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">4.</span><span class="p">,</span><span class="mf">2.</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">3.</span><span class="p">,</span><span class="mf">8.</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">))</span>     <span class="c1"># returns a 2D array</span>
<span class="go">array([[ 4., 3.],</span>
<span class="go">       [ 2., 8.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">hstack</span><span class="p">((</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">))</span>           <span class="c1"># the result is different</span>
<span class="go">array([ 4., 2., 3., 8.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:,</span><span class="n">newaxis</span><span class="p">]</span>               <span class="c1"># this allows to have a 2D columns vector</span>
<span class="go">array([[ 4.],</span>
<span class="go">       [ 2.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">a</span><span class="p">[:,</span><span class="n">newaxis</span><span class="p">],</span><span class="n">b</span><span class="p">[:,</span><span class="n">newaxis</span><span class="p">]))</span>
<span class="go">array([[ 4.,  3.],</span>
<span class="go">       [ 2.,  8.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">hstack</span><span class="p">((</span><span class="n">a</span><span class="p">[:,</span><span class="n">newaxis</span><span class="p">],</span><span class="n">b</span><span class="p">[:,</span><span class="n">newaxis</span><span class="p">]))</span>   <span class="c1"># the result is the same</span>
<span class="go">array([[ 4.,  3.],</span>
<span class="go">       [ 2.,  8.]])</span>
</pre></div>
</div>
<p>On the other hand, the function <a class="reference internal" href="../reference/generated/numpy.ma.row_stack.html#numpy.ma.row_stack" title="numpy.ma.row_stack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ma.row_stack</span></code></a> is equivalent to <a class="reference internal" href="../reference/generated/numpy.vstack.html#numpy.vstack" title="numpy.vstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vstack</span></code></a>
for any input arrays.
In general, for arrays with more than two dimensions,
<a class="reference internal" href="../reference/generated/numpy.hstack.html#numpy.hstack" title="numpy.hstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hstack</span></code></a> stacks along their second
axes, <a class="reference internal" href="../reference/generated/numpy.vstack.html#numpy.vstack" title="numpy.vstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vstack</span></code></a> stacks along their
first axes, and <a class="reference internal" href="../reference/generated/numpy.concatenate.html#numpy.concatenate" title="numpy.concatenate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">concatenate</span></code></a>
allows for an optional arguments giving the number of the axis along
which the concatenation should happen.</p>
<p><strong>Note</strong></p>
<p>In complex cases, <a class="reference internal" href="../reference/generated/numpy.r_.html#numpy.r_" title="numpy.r_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">r_</span></code></a> and
<a class="reference internal" href="../reference/generated/numpy.c_.html#numpy.c_" title="numpy.c_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">c_</span></code></a> are useful for creating arrays
by stacking numbers along one axis. They allow the use of range literals
(“:”)</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">r_</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="go">array([1, 2, 3, 0, 4])</span>
</pre></div>
</div>
<p>When used with arrays as arguments,
<a class="reference internal" href="../reference/generated/numpy.r_.html#numpy.r_" title="numpy.r_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">r_</span></code></a> and
<a class="reference internal" href="../reference/generated/numpy.c_.html#numpy.c_" title="numpy.c_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">c_</span></code></a> are similar to
<a class="reference internal" href="../reference/generated/numpy.vstack.html#numpy.vstack" title="numpy.vstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vstack</span></code></a> and
<a class="reference internal" href="../reference/generated/numpy.hstack.html#numpy.hstack" title="numpy.hstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hstack</span></code></a> in their default behavior,
but allow for an optional argument giving the number of the axis along
which to concatenate.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="../reference/generated/numpy.hstack.html#numpy.hstack" title="numpy.hstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hstack</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.vstack.html#numpy.vstack" title="numpy.vstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vstack</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.column_stack.html#numpy.column_stack" title="numpy.column_stack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">column_stack</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.concatenate.html#numpy.concatenate" title="numpy.concatenate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">concatenate</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.c_.html#numpy.c_" title="numpy.c_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">c_</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.r_.html#numpy.r_" title="numpy.r_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">r_</span></code></a></p>
</div>
</div>
<div class="section" id="splitting-one-array-into-several-smaller-ones">
<h3>Splitting one array into several smaller ones<a class="headerlink" href="#splitting-one-array-into-several-smaller-ones" title="Permalink to this headline">¶</a></h3>
<p>Using <a class="reference internal" href="../reference/generated/numpy.hsplit.html#numpy.hsplit" title="numpy.hsplit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hsplit</span></code></a>, you can split an
array along its horizontal axis, either by specifying the number of
equally shaped arrays to return, or by specifying the columns after
which the division should occur:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="mi">10</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span><span class="mi">12</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 9.,  5.,  6.,  3.,  6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],</span>
<span class="go">       [ 1.,  4.,  9.,  2.,  2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">hsplit</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>   <span class="c1"># Split a into 3</span>
<span class="go">[array([[ 9.,  5.,  6.,  3.],</span>
<span class="go">       [ 1.,  4.,  9.,  2.]]), array([[ 6.,  8.,  0.,  7.],</span>
<span class="go">       [ 2.,  1.,  0.,  6.]]), array([[ 9.,  7.,  2.,  7.],</span>
<span class="go">       [ 2.,  2.,  4.,  0.]])]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">hsplit</span><span class="p">(</span><span class="n">a</span><span class="p">,(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">))</span>   <span class="c1"># Split a after the third and the fourth column</span>
<span class="go">[array([[ 9.,  5.,  6.],</span>
<span class="go">       [ 1.,  4.,  9.]]), array([[ 3.],</span>
<span class="go">       [ 2.]]), array([[ 6.,  8.,  0.,  7.,  9.,  7.,  2.,  7.],</span>
<span class="go">       [ 2.,  1.,  0.,  6.,  2.,  2.,  4.,  0.]])]</span>
</pre></div>
</div>
<p><a class="reference internal" href="../reference/generated/numpy.vsplit.html#numpy.vsplit" title="numpy.vsplit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vsplit</span></code></a> splits along the vertical
axis, and <a class="reference internal" href="../reference/generated/numpy.array_split.html#numpy.array_split" title="numpy.array_split"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array_split</span></code></a> allows
one to specify along which axis to split.</p>
</div>
</div>
<div class="section" id="copies-and-views">
<h2>Copies and Views<a class="headerlink" href="#copies-and-views" title="Permalink to this headline">¶</a></h2>
<p>When operating and manipulating arrays, their data is sometimes copied
into a new array and sometimes not. This is often a source of confusion
for beginners. There are three cases:</p>
<div class="section" id="no-copy-at-all">
<h3>No Copy at All<a class="headerlink" href="#no-copy-at-all" title="Permalink to this headline">¶</a></h3>
<p>Simple assignments make no copy of array objects or of their data.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span>            <span class="c1"># no new object is created</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="ow">is</span> <span class="n">a</span>           <span class="c1"># a and b are two names for the same ndarray object</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span><span class="mi">4</span>    <span class="c1"># changes the shape of a</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 4)</span>
</pre></div>
</div>
<p>Python passes mutable objects as references, so function calls make no
copy.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">id</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>                           <span class="c1"># id is a unique identifier of an object</span>
<span class="go">148293216</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">148293216</span>
</pre></div>
</div>
</div>
<div class="section" id="view-or-shallow-copy">
<h3>View or Shallow Copy<a class="headerlink" href="#view-or-shallow-copy" title="Permalink to this headline">¶</a></h3>
<p>Different array objects can share the same data. The <code class="docutils literal notranslate"><span class="pre">view</span></code> method
creates a new array object that looks at the same data.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">view</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="ow">is</span> <span class="n">a</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">base</span> <span class="ow">is</span> <span class="n">a</span>                        <span class="c1"># c is a view of the data owned by a</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">flags</span><span class="o">.</span><span class="n">owndata</span>
<span class="go">False</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span><span class="mi">6</span>                      <span class="c1"># a&#39;s shape doesn&#39;t change</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(3, 4)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1234</span>                      <span class="c1"># a&#39;s data changes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[   0,    1,    2,    3],</span>
<span class="go">       [1234,    5,    6,    7],</span>
<span class="go">       [   8,    9,   10,   11]])</span>
</pre></div>
</div>
<p>Slicing an array returns a view of it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span> <span class="p">:</span> <span class="p">,</span> <span class="mi">1</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>     <span class="c1"># spaces added for clarity; could also be written &quot;s = a[:,1:3]&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="p">[:]</span> <span class="o">=</span> <span class="mi">10</span>           <span class="c1"># s[:] is a view of s. Note the difference between s=10 and s[:]=10</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[   0,   10,   10,    3],</span>
<span class="go">       [1234,   10,   10,    7],</span>
<span class="go">       [   8,   10,   10,   11]])</span>
</pre></div>
</div>
</div>
<div class="section" id="deep-copy">
<h3>Deep Copy<a class="headerlink" href="#deep-copy" title="Permalink to this headline">¶</a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">copy</span></code> method makes a complete copy of the array and its data.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>                          <span class="c1"># a new array object with new data is created</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="ow">is</span> <span class="n">a</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">base</span> <span class="ow">is</span> <span class="n">a</span>                           <span class="c1"># d doesn&#39;t share anything with a</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">9999</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[   0,   10,   10,    3],</span>
<span class="go">       [1234,   10,   10,    7],</span>
<span class="go">       [   8,   10,   10,   11]])</span>
</pre></div>
</div>
<p>Sometimes <code class="docutils literal notranslate"><span class="pre">copy</span></code> should be called after slicing if the original array is not required anymore.
For example, suppose <code class="docutils literal notranslate"><span class="pre">a</span></code> is a huge intermediate result and the final result <code class="docutils literal notranslate"><span class="pre">b</span></code> only contains
a small fraction of <code class="docutils literal notranslate"><span class="pre">a</span></code>, a deep copy should be made when constructing <code class="docutils literal notranslate"><span class="pre">b</span></code> with slicing:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="mf">1e8</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="p">[:</span><span class="mi">100</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">a</span>  <span class="c1"># the memory of ``a`` can be released.</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">b</span> <span class="pre">=</span> <span class="pre">a[:100]</span></code> is used instead, <code class="docutils literal notranslate"><span class="pre">a</span></code> is referenced by <code class="docutils literal notranslate"><span class="pre">b</span></code> and will persist in memory
even if <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">a</span></code> is executed.</p>
</div>
<div class="section" id="functions-and-methods-overview">
<h3>Functions and Methods Overview<a class="headerlink" href="#functions-and-methods-overview" title="Permalink to this headline">¶</a></h3>
<p>Here is a list of some useful NumPy functions and methods names
ordered in categories. See <a class="reference internal" href="../reference/routines.html#routines"><span class="std std-ref">Routines</span></a> for the full list.</p>
<dl class="simple">
<dt>Array Creation</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.arange.html#numpy.arange" title="numpy.arange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">arange</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.array.html#numpy.array" title="numpy.array"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.copy.html#numpy.copy" title="numpy.copy"><code class="xref py py-obj docutils literal notranslate"><span class="pre">copy</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.empty.html#numpy.empty" title="numpy.empty"><code class="xref py py-obj docutils literal notranslate"><span class="pre">empty</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.empty_like.html#numpy.empty_like" title="numpy.empty_like"><code class="xref py py-obj docutils literal notranslate"><span class="pre">empty_like</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.eye.html#numpy.eye" title="numpy.eye"><code class="xref py py-obj docutils literal notranslate"><span class="pre">eye</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.fromfile.html#numpy.fromfile" title="numpy.fromfile"><code class="xref py py-obj docutils literal notranslate"><span class="pre">fromfile</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.fromfunction.html#numpy.fromfunction" title="numpy.fromfunction"><code class="xref py py-obj docutils literal notranslate"><span class="pre">fromfunction</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.identity.html#numpy.identity" title="numpy.identity"><code class="xref py py-obj docutils literal notranslate"><span class="pre">identity</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.linspace.html#numpy.linspace" title="numpy.linspace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">linspace</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.logspace.html#numpy.logspace" title="numpy.logspace"><code class="xref py py-obj docutils literal notranslate"><span class="pre">logspace</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.mgrid.html#numpy.mgrid" title="numpy.mgrid"><code class="xref py py-obj docutils literal notranslate"><span class="pre">mgrid</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ogrid.html#numpy.ogrid" title="numpy.ogrid"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ogrid</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ones.html#numpy.ones" title="numpy.ones"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ones</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ones_like.html#numpy.ones_like" title="numpy.ones_like"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ones_like</span></code></a>,
<em class="xref py py-obj">r</em>,
<a class="reference internal" href="../reference/generated/numpy.zeros.html#numpy.zeros" title="numpy.zeros"><code class="xref py py-obj docutils literal notranslate"><span class="pre">zeros</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.zeros_like.html#numpy.zeros_like" title="numpy.zeros_like"><code class="xref py py-obj docutils literal notranslate"><span class="pre">zeros_like</span></code></a></p>
</dd>
<dt>Conversions</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.ndarray.astype.html#numpy.ndarray.astype" title="numpy.ndarray.astype"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndarray.astype</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.atleast_1d.html#numpy.atleast_1d" title="numpy.atleast_1d"><code class="xref py py-obj docutils literal notranslate"><span class="pre">atleast_1d</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.atleast_2d.html#numpy.atleast_2d" title="numpy.atleast_2d"><code class="xref py py-obj docutils literal notranslate"><span class="pre">atleast_2d</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.atleast_3d.html#numpy.atleast_3d" title="numpy.atleast_3d"><code class="xref py py-obj docutils literal notranslate"><span class="pre">atleast_3d</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.mat.html#numpy.mat" title="numpy.mat"><code class="xref py py-obj docutils literal notranslate"><span class="pre">mat</span></code></a></p>
</dd>
<dt>Manipulations</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.array_split.html#numpy.array_split" title="numpy.array_split"><code class="xref py py-obj docutils literal notranslate"><span class="pre">array_split</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.column_stack.html#numpy.column_stack" title="numpy.column_stack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">column_stack</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.concatenate.html#numpy.concatenate" title="numpy.concatenate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">concatenate</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.diagonal.html#numpy.diagonal" title="numpy.diagonal"><code class="xref py py-obj docutils literal notranslate"><span class="pre">diagonal</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.dsplit.html#numpy.dsplit" title="numpy.dsplit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dsplit</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.dstack.html#numpy.dstack" title="numpy.dstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dstack</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.hsplit.html#numpy.hsplit" title="numpy.hsplit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hsplit</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.hstack.html#numpy.hstack" title="numpy.hstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">hstack</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ndarray.item.html#numpy.ndarray.item" title="numpy.ndarray.item"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndarray.item</span></code></a>,
<a class="reference internal" href="../reference/constants.html#numpy.newaxis" title="numpy.newaxis"><code class="xref py py-obj docutils literal notranslate"><span class="pre">newaxis</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ravel.html#numpy.ravel" title="numpy.ravel"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ravel</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.repeat.html#numpy.repeat" title="numpy.repeat"><code class="xref py py-obj docutils literal notranslate"><span class="pre">repeat</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.reshape.html#numpy.reshape" title="numpy.reshape"><code class="xref py py-obj docutils literal notranslate"><span class="pre">reshape</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.resize.html#numpy.resize" title="numpy.resize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">resize</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.squeeze.html#numpy.squeeze" title="numpy.squeeze"><code class="xref py py-obj docutils literal notranslate"><span class="pre">squeeze</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.swapaxes.html#numpy.swapaxes" title="numpy.swapaxes"><code class="xref py py-obj docutils literal notranslate"><span class="pre">swapaxes</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.take.html#numpy.take" title="numpy.take"><code class="xref py py-obj docutils literal notranslate"><span class="pre">take</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.transpose.html#numpy.transpose" title="numpy.transpose"><code class="xref py py-obj docutils literal notranslate"><span class="pre">transpose</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.vsplit.html#numpy.vsplit" title="numpy.vsplit"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vsplit</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.vstack.html#numpy.vstack" title="numpy.vstack"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vstack</span></code></a></p>
</dd>
<dt>Questions</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.all.html#numpy.all" title="numpy.all"><code class="xref py py-obj docutils literal notranslate"><span class="pre">all</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.any.html#numpy.any" title="numpy.any"><code class="xref py py-obj docutils literal notranslate"><span class="pre">any</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.nonzero.html#numpy.nonzero" title="numpy.nonzero"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nonzero</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.where.html#numpy.where" title="numpy.where"><code class="xref py py-obj docutils literal notranslate"><span class="pre">where</span></code></a></p>
</dd>
<dt>Ordering</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.argmax.html#numpy.argmax" title="numpy.argmax"><code class="xref py py-obj docutils literal notranslate"><span class="pre">argmax</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.argmin.html#numpy.argmin" title="numpy.argmin"><code class="xref py py-obj docutils literal notranslate"><span class="pre">argmin</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.argsort.html#numpy.argsort" title="numpy.argsort"><code class="xref py py-obj docutils literal notranslate"><span class="pre">argsort</span></code></a>,
<a class="reference external" href="https://docs.python.org/dev/library/functions.html#max" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">max</span></code></a>,
<a class="reference external" href="https://docs.python.org/dev/library/functions.html#min" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">min</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ptp.html#numpy.ptp" title="numpy.ptp"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ptp</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.searchsorted.html#numpy.searchsorted" title="numpy.searchsorted"><code class="xref py py-obj docutils literal notranslate"><span class="pre">searchsorted</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.sort.html#numpy.sort" title="numpy.sort"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sort</span></code></a></p>
</dd>
<dt>Operations</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.choose.html#numpy.choose" title="numpy.choose"><code class="xref py py-obj docutils literal notranslate"><span class="pre">choose</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.compress.html#numpy.compress" title="numpy.compress"><code class="xref py py-obj docutils literal notranslate"><span class="pre">compress</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.cumprod.html#numpy.cumprod" title="numpy.cumprod"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cumprod</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.cumsum.html#numpy.cumsum" title="numpy.cumsum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cumsum</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.inner.html#numpy.inner" title="numpy.inner"><code class="xref py py-obj docutils literal notranslate"><span class="pre">inner</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.ndarray.fill.html#numpy.ndarray.fill" title="numpy.ndarray.fill"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ndarray.fill</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.imag.html#numpy.imag" title="numpy.imag"><code class="xref py py-obj docutils literal notranslate"><span class="pre">imag</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.prod.html#numpy.prod" title="numpy.prod"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prod</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.put.html#numpy.put" title="numpy.put"><code class="xref py py-obj docutils literal notranslate"><span class="pre">put</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.putmask.html#numpy.putmask" title="numpy.putmask"><code class="xref py py-obj docutils literal notranslate"><span class="pre">putmask</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.real.html#numpy.real" title="numpy.real"><code class="xref py py-obj docutils literal notranslate"><span class="pre">real</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.sum.html#numpy.sum" title="numpy.sum"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sum</span></code></a></p>
</dd>
<dt>Basic Statistics</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.cov.html#numpy.cov" title="numpy.cov"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cov</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.mean.html#numpy.mean" title="numpy.mean"><code class="xref py py-obj docutils literal notranslate"><span class="pre">mean</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.std.html#numpy.std" title="numpy.std"><code class="xref py py-obj docutils literal notranslate"><span class="pre">std</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.var.html#numpy.var" title="numpy.var"><code class="xref py py-obj docutils literal notranslate"><span class="pre">var</span></code></a></p>
</dd>
<dt>Basic Linear Algebra</dt><dd><p><a class="reference internal" href="../reference/generated/numpy.cross.html#numpy.cross" title="numpy.cross"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cross</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.dot.html#numpy.dot" title="numpy.dot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">dot</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.outer.html#numpy.outer" title="numpy.outer"><code class="xref py py-obj docutils literal notranslate"><span class="pre">outer</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.linalg.svd.html#numpy.linalg.svd" title="numpy.linalg.svd"><code class="xref py py-obj docutils literal notranslate"><span class="pre">linalg.svd</span></code></a>,
<a class="reference internal" href="../reference/generated/numpy.vdot.html#numpy.vdot" title="numpy.vdot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">vdot</span></code></a></p>
</dd>
</dl>
</div>
</div>
<div class="section" id="less-basic">
<h2>Less Basic<a class="headerlink" href="#less-basic" title="Permalink to this headline">¶</a></h2>
<div class="section" id="broadcasting-rules">
<h3>Broadcasting rules<a class="headerlink" href="#broadcasting-rules" title="Permalink to this headline">¶</a></h3>
<p>Broadcasting allows universal functions to deal in a meaningful way with
inputs that do not have exactly the same shape.</p>
<p>The first rule of broadcasting is that if all input arrays do not have
the same number of dimensions, a “1” will be repeatedly prepended to the
shapes of the smaller arrays until all the arrays have the same number
of dimensions.</p>
<p>The second rule of broadcasting ensures that arrays with a size of 1
along a particular dimension act as if they had the size of the array
with the largest shape along that dimension. The value of the array
element is assumed to be the same along that dimension for the
“broadcast” array.</p>
<p>After application of the broadcasting rules, the sizes of all arrays
must match. More details can be found in <a class="reference internal" href="basics.broadcasting.html"><span class="doc">Broadcasting</span></a>.</p>
</div>
</div>
<div class="section" id="fancy-indexing-and-index-tricks">
<h2>Fancy indexing and index tricks<a class="headerlink" href="#fancy-indexing-and-index-tricks" title="Permalink to this headline">¶</a></h2>
<p>NumPy offers more indexing facilities than regular Python sequences. In
addition to indexing by integers and slices, as we saw before, arrays
can be indexed by arrays of integers and arrays of booleans.</p>
<div class="section" id="indexing-with-arrays-of-indices">
<h3>Indexing with Arrays of Indices<a class="headerlink" href="#indexing-with-arrays-of-indices" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>                       <span class="c1"># the first 12 square numbers</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">5</span> <span class="p">]</span> <span class="p">)</span>              <span class="c1"># an array of indices</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>                                       <span class="c1"># the elements of a at the positions i</span>
<span class="go">array([ 1,  1,  9, 64, 25])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="p">[</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="p">[</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">7</span> <span class="p">]</span> <span class="p">]</span> <span class="p">)</span>      <span class="c1"># a bidimensional array of indices</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>                                       <span class="c1"># the same shape as j</span>
<span class="go">array([[ 9, 16],</span>
<span class="go">       [81, 49]])</span>
</pre></div>
</div>
<p>When the indexed array <code class="docutils literal notranslate"><span class="pre">a</span></code> is multidimensional, a single array of
indices refers to the first dimension of <code class="docutils literal notranslate"><span class="pre">a</span></code>. The following example
shows this behavior by converting an image of labels into a color image
using a palette.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">palette</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span>                <span class="c1"># black</span>
<span class="gp">... </span>                      <span class="p">[</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span>              <span class="c1"># red</span>
<span class="gp">... </span>                      <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">0</span><span class="p">],</span>              <span class="c1"># green</span>
<span class="gp">... </span>                      <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">255</span><span class="p">],</span>              <span class="c1"># blue</span>
<span class="gp">... </span>                      <span class="p">[</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">,</span><span class="mi">255</span><span class="p">]</span> <span class="p">]</span> <span class="p">)</span>       <span class="c1"># white</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">image</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span> <span class="p">],</span>           <span class="c1"># each value corresponds to a color in the palette</span>
<span class="gp">... </span>                    <span class="p">[</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">0</span> <span class="p">]</span>  <span class="p">]</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">palette</span><span class="p">[</span><span class="n">image</span><span class="p">]</span>                            <span class="c1"># the (2,4,3) color image</span>
<span class="go">array([[[  0,   0,   0],</span>
<span class="go">        [255,   0,   0],</span>
<span class="go">        [  0, 255,   0],</span>
<span class="go">        [  0,   0,   0]],</span>
<span class="go">       [[  0,   0,   0],</span>
<span class="go">        [  0,   0, 255],</span>
<span class="go">        [255, 255, 255],</span>
<span class="go">        [  0,   0,   0]]])</span>
</pre></div>
</div>
<p>We can also give indexes for more than one dimension. The arrays of
indices for each dimension must have the same shape.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[ 0,  1,  2,  3],</span>
<span class="go">       [ 4,  5,  6,  7],</span>
<span class="go">       [ 8,  9, 10, 11]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">i</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span>                        <span class="c1"># indices for the first dim of a</span>
<span class="gp">... </span>                <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span> <span class="p">]</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span>                        <span class="c1"># indices for the second dim</span>
<span class="gp">... </span>                <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span> <span class="p">]</span> <span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span>                                     <span class="c1"># i and j must have equal shape</span>
<span class="go">array([[ 2,  5],</span>
<span class="go">       [ 7, 11]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span>
<span class="go">array([[ 2,  6],</span>
<span class="go">       [ 6, 10]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:,</span><span class="n">j</span><span class="p">]</span>                                     <span class="c1"># i.e., a[ : , j]</span>
<span class="go">array([[[ 2,  1],</span>
<span class="go">        [ 3,  3]],</span>
<span class="go">       [[ 6,  5],</span>
<span class="go">        [ 7,  7]],</span>
<span class="go">       [[10,  9],</span>
<span class="go">        [11, 11]]])</span>
</pre></div>
</div>
<p>Naturally, we can put <code class="docutils literal notranslate"><span class="pre">i</span></code> and <code class="docutils literal notranslate"><span class="pre">j</span></code> in a sequence (say a list) and
then do the indexing with the list.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">l</span><span class="p">]</span>                                       <span class="c1"># equivalent to a[i,j]</span>
<span class="go">array([[ 2,  5],</span>
<span class="go">       [ 7, 11]])</span>
</pre></div>
</div>
<p>However, we can not do this by putting <code class="docutils literal notranslate"><span class="pre">i</span></code> and <code class="docutils literal notranslate"><span class="pre">j</span></code> into an array,
because this array will be interpreted as indexing the first dimension
of a.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span> <span class="p">[</span><span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">]</span> <span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">s</span><span class="p">]</span>                                       <span class="c1"># not what we want</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">?</span>
<span class="gr">IndexError</span>: <span class="n">index (3) out of range (0&lt;=index&lt;=2) in dimension 0</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">s</span><span class="p">)]</span>                                <span class="c1"># same as a[i,j]</span>
<span class="go">array([[ 2,  5],</span>
<span class="go">       [ 7, 11]])</span>
</pre></div>
</div>
<p>Another common use of indexing with arrays is the search of the maximum
value of time-dependent series:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">145</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>                 <span class="c1"># time scale</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">20</span><span class="p">))</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>      <span class="c1"># 4 time-dependent series</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time</span>
<span class="go">array([  20.  ,   51.25,   82.5 ,  113.75,  145.  ])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">array([[ 0.        ,  0.84147098,  0.90929743,  0.14112001],</span>
<span class="go">       [-0.7568025 , -0.95892427, -0.2794155 ,  0.6569866 ],</span>
<span class="go">       [ 0.98935825,  0.41211849, -0.54402111, -0.99999021],</span>
<span class="go">       [-0.53657292,  0.42016704,  0.99060736,  0.65028784],</span>
<span class="go">       [-0.28790332, -0.96139749, -0.75098725,  0.14987721]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ind</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">argmax</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>                  <span class="c1"># index of the maxima for each series</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ind</span>
<span class="go">array([2, 0, 3, 1])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time_max</span> <span class="o">=</span> <span class="n">time</span><span class="p">[</span><span class="n">ind</span><span class="p">]</span>                       <span class="c1"># times corresponding to the maxima</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data_max</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">ind</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span> <span class="c1"># =&gt; data[ind[0],0], data[ind[1],1]...</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">time_max</span>
<span class="go">array([  82.5 ,   20.  ,  113.75,   51.25])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data_max</span>
<span class="go">array([ 0.98935825,  0.84147098,  0.99060736,  0.6569866 ])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">all</span><span class="p">(</span><span class="n">data_max</span> <span class="o">==</span> <span class="n">data</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>You can also use indexing with arrays as a target to assign to:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([0, 1, 2, 3, 4])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[[</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">]]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([0, 0, 2, 0, 0])</span>
</pre></div>
</div>
<p>However, when the list of indices contains repetitions, the assignment
is done several times, leaving behind the last value:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">]]</span><span class="o">=</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([2, 1, 3, 3, 4])</span>
</pre></div>
</div>
<p>This is reasonable enough, but watch out if you want to use Python’s
<code class="docutils literal notranslate"><span class="pre">+=</span></code> construct, as it may not do what you expect:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">]]</span><span class="o">+=</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([1, 1, 3, 3, 4])</span>
</pre></div>
</div>
<p>Even though 0 occurs twice in the list of indices, the 0th element is
only incremented once. This is because Python requires “a+=1” to be
equivalent to “a = a + 1”.</p>
</div>
<div class="section" id="indexing-with-boolean-arrays">
<h3>Indexing with Boolean Arrays<a class="headerlink" href="#indexing-with-boolean-arrays" title="Permalink to this headline">¶</a></h3>
<p>When we index arrays with arrays of (integer) indices we are providing
the list of indices to pick. With boolean indices the approach is
different; we explicitly choose which items in the array we want and
which ones we don’t.</p>
<p>The most natural way one can think of for boolean indexing is to use
boolean arrays that have <em>the same shape</em> as the original array:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="mi">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>                                          <span class="c1"># b is a boolean with a&#39;s shape</span>
<span class="go">array([[False, False, False, False],</span>
<span class="go">       [False,  True,  True,  True],</span>
<span class="go">       [ True,  True,  True,  True]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">]</span>                                       <span class="c1"># 1d array with the selected elements</span>
<span class="go">array([ 5,  6,  7,  8,  9, 10, 11])</span>
</pre></div>
</div>
<p>This property can be very useful in assignments:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>                                   <span class="c1"># All elements of &#39;a&#39; higher than 4 become 0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[0, 1, 2, 3],</span>
<span class="go">       [4, 0, 0, 0],</span>
<span class="go">       [0, 0, 0, 0]])</span>
</pre></div>
</div>
<p>You can look at the following
example to see
how to use boolean indexing to generate an image of the <a class="reference external" href="https://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot
set</a>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">mandelbrot</span><span class="p">(</span> <span class="n">h</span><span class="p">,</span><span class="n">w</span><span class="p">,</span> <span class="n">maxit</span><span class="o">=</span><span class="mi">20</span> <span class="p">):</span>
<span class="gp">... </span>    <span class="sd">&quot;&quot;&quot;Returns an image of the Mandelbrot fractal of size (h,w).&quot;&quot;&quot;</span>
<span class="gp">... </span>    <span class="n">y</span><span class="p">,</span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ogrid</span><span class="p">[</span> <span class="o">-</span><span class="mf">1.4</span><span class="p">:</span><span class="mf">1.4</span><span class="p">:</span><span class="n">h</span><span class="o">*</span><span class="mi">1</span><span class="n">j</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">:</span><span class="mf">0.8</span><span class="p">:</span><span class="n">w</span><span class="o">*</span><span class="mi">1</span><span class="n">j</span> <span class="p">]</span>
<span class="gp">... </span>    <span class="n">c</span> <span class="o">=</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">*</span><span class="mi">1</span><span class="n">j</span>
<span class="gp">... </span>    <span class="n">z</span> <span class="o">=</span> <span class="n">c</span>
<span class="gp">... </span>    <span class="n">divtime</span> <span class="o">=</span> <span class="n">maxit</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">z</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">int</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">maxit</span><span class="p">):</span>
<span class="gp">... </span>        <span class="n">z</span> <span class="o">=</span> <span class="n">z</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">c</span>
<span class="gp">... </span>        <span class="n">diverge</span> <span class="o">=</span> <span class="n">z</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">conj</span><span class="p">(</span><span class="n">z</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="o">**</span><span class="mi">2</span>            <span class="c1"># who is diverging</span>
<span class="gp">... </span>        <span class="n">div_now</span> <span class="o">=</span> <span class="n">diverge</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">divtime</span><span class="o">==</span><span class="n">maxit</span><span class="p">)</span>  <span class="c1"># who is diverging now</span>
<span class="gp">... </span>        <span class="n">divtime</span><span class="p">[</span><span class="n">div_now</span><span class="p">]</span> <span class="o">=</span> <span class="n">i</span>                  <span class="c1"># note when</span>
<span class="gp">... </span>        <span class="n">z</span><span class="p">[</span><span class="n">diverge</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>                        <span class="c1"># avoid diverging too much</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">divtime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">mandelbrot</span><span class="p">(</span><span class="mi">400</span><span class="p">,</span><span class="mi">400</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default">
<img alt="../_images/quickstart-1.png" src="../_images/quickstart-1.png" />
</div>
<p>The second way of indexing with booleans is more similar to integer
indexing; for each dimension of the array we give a 1D boolean array
selecting the slices we want:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">False</span><span class="p">,</span><span class="kc">True</span><span class="p">,</span><span class="kc">True</span><span class="p">])</span>             <span class="c1"># first dim selection</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="kc">True</span><span class="p">,</span><span class="kc">False</span><span class="p">,</span><span class="kc">True</span><span class="p">,</span><span class="kc">False</span><span class="p">])</span>       <span class="c1"># second dim selection</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">b1</span><span class="p">,:]</span>                                   <span class="c1"># selecting rows</span>
<span class="go">array([[ 4,  5,  6,  7],</span>
<span class="go">       [ 8,  9, 10, 11]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">b1</span><span class="p">]</span>                                     <span class="c1"># same thing</span>
<span class="go">array([[ 4,  5,  6,  7],</span>
<span class="go">       [ 8,  9, 10, 11]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[:,</span><span class="n">b2</span><span class="p">]</span>                                   <span class="c1"># selecting columns</span>
<span class="go">array([[ 0,  2],</span>
<span class="go">       [ 4,  6],</span>
<span class="go">       [ 8, 10]])</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="n">b1</span><span class="p">,</span><span class="n">b2</span><span class="p">]</span>                                  <span class="c1"># a weird thing to do</span>
<span class="go">array([ 4, 10])</span>
</pre></div>
</div>
<p>Note that the length of the 1D boolean array must coincide with the
length of the dimension (or axis) you want to slice. In the previous
example, <code class="docutils literal notranslate"><span class="pre">b1</span></code> has length 3 (the number of <em>rows</em> in <code class="docutils literal notranslate"><span class="pre">a</span></code>), and
<code class="docutils literal notranslate"><span class="pre">b2</span></code> (of length 4) is suitable to index the 2nd axis (columns) of
<code class="docutils literal notranslate"><span class="pre">a</span></code>.</p>
</div>
<div class="section" id="the-ix-function">
<h3>The ix_() function<a class="headerlink" href="#the-ix-function" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="../reference/generated/numpy.ix_.html#numpy.ix_" title="numpy.ix_"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ix_</span></code></a> function can be used to combine different vectors so as to
obtain the result for each n-uplet. For example, if you want to compute
all the a+b*c for all the triplets taken from each of the vectors a, b
and c:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">8</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax</span><span class="p">,</span><span class="n">bx</span><span class="p">,</span><span class="n">cx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ix_</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax</span>
<span class="go">array([[[2]],</span>
<span class="go">       [[3]],</span>
<span class="go">       [[4]],</span>
<span class="go">       [[5]]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bx</span>
<span class="go">array([[[8],</span>
<span class="go">        [5],</span>
<span class="go">        [4]]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cx</span>
<span class="go">array([[[5, 4, 6, 8, 3]]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">bx</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">cx</span><span class="o">.</span><span class="n">shape</span>
<span class="go">((4, 1, 1), (1, 3, 1), (1, 1, 5))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">result</span> <span class="o">=</span> <span class="n">ax</span><span class="o">+</span><span class="n">bx</span><span class="o">*</span><span class="n">cx</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">result</span>
<span class="go">array([[[42, 34, 50, 66, 26],</span>
<span class="go">        [27, 22, 32, 42, 17],</span>
<span class="go">        [22, 18, 26, 34, 14]],</span>
<span class="go">       [[43, 35, 51, 67, 27],</span>
<span class="go">        [28, 23, 33, 43, 18],</span>
<span class="go">        [23, 19, 27, 35, 15]],</span>
<span class="go">       [[44, 36, 52, 68, 28],</span>
<span class="go">        [29, 24, 34, 44, 19],</span>
<span class="go">        [24, 20, 28, 36, 16]],</span>
<span class="go">       [[45, 37, 53, 69, 29],</span>
<span class="go">        [30, 25, 35, 45, 20],</span>
<span class="go">        [25, 21, 29, 37, 17]]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span>
<span class="go">17</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">+</span><span class="n">b</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">c</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
<span class="go">17</span>
</pre></div>
</div>
<p>You could also implement the reduce as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">ufunc_reduce</span><span class="p">(</span><span class="n">ufct</span><span class="p">,</span> <span class="o">*</span><span class="n">vectors</span><span class="p">):</span>
<span class="gp">... </span>   <span class="n">vs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ix_</span><span class="p">(</span><span class="o">*</span><span class="n">vectors</span><span class="p">)</span>
<span class="gp">... </span>   <span class="n">r</span> <span class="o">=</span> <span class="n">ufct</span><span class="o">.</span><span class="n">identity</span>
<span class="gp">... </span>   <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">vs</span><span class="p">:</span>
<span class="gp">... </span>       <span class="n">r</span> <span class="o">=</span> <span class="n">ufct</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="n">v</span><span class="p">)</span>
<span class="gp">... </span>   <span class="k">return</span> <span class="n">r</span>
</pre></div>
</div>
<p>and then use it as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ufunc_reduce</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="p">,</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">)</span>
<span class="go">array([[[15, 14, 16, 18, 13],</span>
<span class="go">        [12, 11, 13, 15, 10],</span>
<span class="go">        [11, 10, 12, 14,  9]],</span>
<span class="go">       [[16, 15, 17, 19, 14],</span>
<span class="go">        [13, 12, 14, 16, 11],</span>
<span class="go">        [12, 11, 13, 15, 10]],</span>
<span class="go">       [[17, 16, 18, 20, 15],</span>
<span class="go">        [14, 13, 15, 17, 12],</span>
<span class="go">        [13, 12, 14, 16, 11]],</span>
<span class="go">       [[18, 17, 19, 21, 16],</span>
<span class="go">        [15, 14, 16, 18, 13],</span>
<span class="go">        [14, 13, 15, 17, 12]]])</span>
</pre></div>
</div>
<p>The advantage of this version of reduce compared to the normal
ufunc.reduce is that it makes use of the <a class="reference external" href="Tentative_NumPy_Tutorial.html#head-c43f3f81719d84f09ae2b33a22eaf50b26333db8">Broadcasting
Rules</a>
in order to avoid creating an argument array the size of the output
times the number of vectors.</p>
</div>
<div class="section" id="indexing-with-strings">
<h3>Indexing with strings<a class="headerlink" href="#indexing-with-strings" title="Permalink to this headline">¶</a></h3>
<p>See <a class="reference internal" href="basics.rec.html#structured-arrays"><span class="std std-ref">Structured arrays</span></a>.</p>
</div>
</div>
<div class="section" id="linear-algebra">
<h2>Linear Algebra<a class="headerlink" href="#linear-algebra" title="Permalink to this headline">¶</a></h2>
<p>Work in progress. Basic linear algebra to be included here.</p>
<div class="section" id="simple-array-operations">
<h3>Simple Array Operations<a class="headerlink" href="#simple-array-operations" title="Permalink to this headline">¶</a></h3>
<p>See linalg.py in numpy folder for more.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">[[ 1.  2.]</span>
<span class="go"> [ 3.  4.]]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">transpose</span><span class="p">()</span>
<span class="go">array([[ 1.,  3.],</span>
<span class="go">       [ 2.,  4.]])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">inv</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">array([[-2. ,  1. ],</span>
<span class="go">       [ 1.5, -0.5]])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># unit 2x2 matrix; &quot;eye&quot; represents &quot;I&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">u</span>
<span class="go">array([[ 1.,  0.],</span>
<span class="go">       [ 0.,  1.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">j</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">]])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">j</span> <span class="o">@</span> <span class="n">j</span>        <span class="c1"># matrix product</span>
<span class="go">array([[-1.,  0.],</span>
<span class="go">       [ 0., -1.]])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">u</span><span class="p">)</span>  <span class="c1"># trace</span>
<span class="go">2.0</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">5.</span><span class="p">],</span> <span class="p">[</span><span class="mf">7.</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">solve</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="go">array([[-3.],</span>
<span class="go">       [ 4.]])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">eig</span><span class="p">(</span><span class="n">j</span><span class="p">)</span>
<span class="go">(array([ 0.+1.j,  0.-1.j]), array([[ 0.70710678+0.j        ,  0.70710678-0.j        ],</span>
<span class="go">       [ 0.00000000-0.70710678j,  0.00000000+0.70710678j]]))</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>Parameters:
    square matrix
Returns
    The eigenvalues, each repeated according to its multiplicity.
    The normalized (unit &quot;length&quot;) eigenvectors, such that the
    column ``v[:,i]`` is the eigenvector corresponding to the
    eigenvalue ``w[i]`` .
</pre></div>
</div>
</div>
</div>
<div class="section" id="tricks-and-tips">
<h2>Tricks and Tips<a class="headerlink" href="#tricks-and-tips" title="Permalink to this headline">¶</a></h2>
<p>Here we give a list of short and useful tips.</p>
<div class="section" id="automatic-reshaping">
<h3>“Automatic” Reshaping<a class="headerlink" href="#automatic-reshaping" title="Permalink to this headline">¶</a></h3>
<p>To change the dimensions of an array, you can omit one of the sizes
which will then be deduced automatically:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span>  <span class="c1"># -1 means &quot;whatever is needed&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">shape</span>
<span class="go">(2, 5, 3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
<span class="go">array([[[ 0,  1,  2],</span>
<span class="go">        [ 3,  4,  5],</span>
<span class="go">        [ 6,  7,  8],</span>
<span class="go">        [ 9, 10, 11],</span>
<span class="go">        [12, 13, 14]],</span>
<span class="go">       [[15, 16, 17],</span>
<span class="go">        [18, 19, 20],</span>
<span class="go">        [21, 22, 23],</span>
<span class="go">        [24, 25, 26],</span>
<span class="go">        [27, 28, 29]]])</span>
</pre></div>
</div>
</div>
<div class="section" id="vector-stacking">
<h3>Vector Stacking<a class="headerlink" href="#vector-stacking" title="Permalink to this headline">¶</a></h3>
<p>How do we construct a 2D array from a list of equally-sized row vectors?
In MATLAB this is quite easy: if <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> are two vectors of the
same length you only need do <code class="docutils literal notranslate"><span class="pre">m=[x;y]</span></code>. In NumPy this works via the
functions <code class="docutils literal notranslate"><span class="pre">column_stack</span></code>, <code class="docutils literal notranslate"><span class="pre">dstack</span></code>, <code class="docutils literal notranslate"><span class="pre">hstack</span></code> and <code class="docutils literal notranslate"><span class="pre">vstack</span></code>,
depending on the dimension in which the stacking is to be done. For
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>                     <span class="c1"># x=([0,2,4,6,8])</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>                          <span class="c1"># y=([0,1,2,3,4])</span>
<span class="n">m</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">])</span>                      <span class="c1"># m=([[0,2,4,6,8],</span>
                                          <span class="c1">#     [0,1,2,3,4]])</span>
<span class="n">xy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">hstack</span><span class="p">([</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">])</span>                     <span class="c1"># xy =([0,2,4,6,8,0,1,2,3,4])</span>
</pre></div>
</div>
<p>The logic behind those functions in more than two dimensions can be
strange.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="numpy-for-matlab-users.html"><span class="doc">NumPy for Matlab users</span></a></p>
</div>
</div>
<div class="section" id="histograms">
<h3>Histograms<a class="headerlink" href="#histograms" title="Permalink to this headline">¶</a></h3>
<p>The NumPy <code class="docutils literal notranslate"><span class="pre">histogram</span></code> function applied to an array returns a pair of
vectors: the histogram of the array and the vector of bins. Beware:
<code class="docutils literal notranslate"><span class="pre">matplotlib</span></code> also has a function to build histograms (called <code class="docutils literal notranslate"><span class="pre">hist</span></code>,
as in Matlab) that differs from the one in NumPy. The main difference is
that <code class="docutils literal notranslate"><span class="pre">pylab.hist</span></code> plots the histogram automatically, while
<code class="docutils literal notranslate"><span class="pre">numpy.histogram</span></code> only generates the data.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Build a vector of 10000 normal deviates with variance 0.5^2 and mean 2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mu</span><span class="p">,</span> <span class="n">sigma</span> <span class="o">=</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">0.5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">mu</span><span class="p">,</span><span class="n">sigma</span><span class="p">,</span><span class="mi">10000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Plot a normalized histogram with 50 bins</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>       <span class="c1"># matplotlib version (plot)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default">
<img alt="../_images/quickstart-2_00_00.png" src="../_images/quickstart-2_00_00.png" />
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Compute the histogram with numpy and then plot it</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">bins</span><span class="p">)</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">histogram</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>  <span class="c1"># NumPy version (no plot)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="o">.</span><span class="mi">5</span><span class="o">*</span><span class="p">(</span><span class="n">bins</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span><span class="o">+</span><span class="n">bins</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]),</span> <span class="n">n</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<div class="figure align-default">
<img alt="../_images/quickstart-2_01_00.png" src="../_images/quickstart-2_01_00.png" />
</div>
</div>
</div>
<div class="section" id="further-reading">
<h2>Further reading<a class="headerlink" href="#further-reading" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><p>The <a class="reference external" href="https://docs.python.org/tutorial/">Python tutorial</a></p></li>
<li><p><a class="reference internal" href="../reference/index.html#reference"><span class="std std-ref">NumPy Reference</span></a></p></li>
<li><p><a class="reference external" href="https://docs.scipy.org/doc/scipy/reference/tutorial/index.html">SciPy Tutorial</a></p></li>
<li><p><a class="reference external" href="https://scipy-lectures.org">SciPy Lecture Notes</a></p></li>
<li><p>A <a class="reference external" href="http://mathesaurus.sf.net/">matlab, R, IDL, NumPy/SciPy dictionary</a></p></li>
</ul>
</div>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>